/**
 * 关卡界面
 */

import Define, { CustomAdType } from "./SdkTools/Define";
import gameNetWorkState from "./SdkTools/gameNetWorkState";
import gamePrefabMgr, { uiPreName } from "./SdkTools/gamePrefabMgr";
import gameShareAdsVideo from "./SdkTools/gameShareAdsVideo";
import PlayData, { GameType } from "./SdkTools/PlayData";
import storage from "./SdkTools/storage";
import TipsManager from "./SdkTools/TipsManager";
import Utils from "./SdkTools/Utils";
import TopUi from "./TopUi";
import SDKManager from "./plat/SDKManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class checkoutPanel extends cc.Component {
    
    @property(cc.Node)
    content1: cc.Node = null

    @property(cc.Node)
    jiesuo: cc.Node = null

    @property(cc.Label)
    pagenum: cc.Label = null

    @property(cc.Node)
    moregamepanel: cc.Node = null

    jiesuoIndex = 0
    pageIndex = 0
    nowPageIndex = 0
    maxPageIndex = 0
    touchPos = null
    cellnum = 0

    onLoad() {
        this.jiesuo.active = !1
        this.pageIndex = 0
        this.nowPageIndex = 0
        this.pagenum.string = '' + (this.pageIndex + 1)
        this.moregamepanel.active = !1
        let levelnum = null
        let _data = storage.Instance.getCheckPoint_diff()
        if (PlayData.Instance.gameType == GameType.finddifference_zhaobutong) {
            this.cellnum = 12
            levelnum = _data[3]
        }
        for (let index = 0; index < levelnum.length; index++) {
            const element = levelnum[index];
            if (element == 0) {
                this.nowPageIndex = Math.floor(index / this.cellnum)
                this.pageIndex = this.nowPageIndex
                break
            }
        }
        this.pagenum.string = '' + (this.nowPageIndex + 1)

        this.initUi()
    }

    start() {
        SDKManager.getInstance().showInterstitial();
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            cc.find('moregame', this.node).active = !0
        } else if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
            cc.find('moregame', this.node).active = !1
        }
    }

    letBtnClick() {
        this.pageIndex--
        if (this.pageIndex < 0) {
            this.pageIndex = 0
        }
        this.pagenum.string = '' + (this.pageIndex + 1)
        if (this.nowPageIndex != this.pageIndex) {
            this.nowPageIndex = this.pageIndex
            this.initUi()
        }
    }
    rightBtnClick() {
        this.pageIndex++
        if (this.pageIndex >= this.maxPageIndex) {
            this.pageIndex = this.maxPageIndex - 1
        }
        this.pagenum.string = '' + (this.pageIndex + 1)
        if (this.nowPageIndex != this.pageIndex) {
            this.nowPageIndex = this.pageIndex
            this.initUi()
        }
    }

    /**
     * 初始化ui
     */
    initUi() {
        if (PlayData.Instance.gameType == GameType.finddifference_zhaobutong) {

            this.content1.removeAllChildren()
            this.cellnum = 12
            this.maxPageIndex = Math.ceil(Define.maxCheckNum_huaijiu / this.cellnum)
            let _data = storage.Instance.getCheckPoint_diff()
            let levelnum = _data[3]
            for (let ind = 0; ind < this.cellnum; ind++) {
                let inn = (this.cellnum * this.nowPageIndex) + ind
                let key = inn
                //console.log('打印key=', key)
                let newitem = cc.instantiate(gamePrefabMgr.Instance.uiPre[uiPreName.check_item2])
                newitem.parent = this.content1
                newitem.name = '' + ind
                newitem.on('click', this.itemClick, this)

                if (key < Define.maxCheckNum_huaijiu) {
                    newitem.active = !0
                } else {
                    newitem.active = !1
                }

                let num = cc.find('num_xiao', newitem).getComponent(cc.Label)
                let pic = cc.find('pic', newitem)
                let suo = cc.find('suo', newitem)
                let wenhao = cc.find('wenhao', newitem)
                num.string = '' + (key + 1)

                if (levelnum[key] == 1) {
                    suo.active = !1
                    pic.active = !0
                    wenhao.active = !1
                    let sp = pic.getComponent(cc.Sprite)
                    cc.resources.load('huaijiu/level' + (key + 1) + '/png' + (key + 1), cc.SpriteFrame, (err, assert) => {
                        if (err) return console.error(assert)
                        sp.spriteFrame = assert as cc.SpriteFrame
                    })
                } else if (levelnum[key] == 0) {
                    suo.active = !1
                    pic.active = !1
                    wenhao.active = !0
                    //newitem.color = cc.Color.GREEN
                } else {
                    suo.active = !0
                    pic.active = !1
                    wenhao.active = !1
                }
            }
        }
    }

    itemClick(event) {
        console.log('item click=', event)

        if (PlayData.Instance.gameType == GameType.finddifference_zhaobutong) {
            //let name = event.node.name
            let ind = Number(event.node.name)
            let _data = storage.Instance.getCheckPoint_diff()
            let levelnum = _data[3]
            let key = (this.cellnum * this.nowPageIndex) + ind
            //console.log('打印数据', this.cellnum, this.nowPageIndex, ind, key)
            if (levelnum[key] == 0 || levelnum[key] == 1) {
                if (storage.Instance.getEnergy() <= 0) {
                    TipsManager.Instance.createTips('体力不足')
                    //console.log('体力不足')
                    let top = cc.find('Canvas/top')
                    let topsc = top.getComponent(TopUi)
                    topsc.openAddEngry()
                } else {
                    storage.Instance.setEnergy(storage.Instance.nowEnery - 1)
                    cc.director.emit('addengry')
                    cc.director.loadScene('GameScene')
                    PlayData.Instance.checkoutNum = key
                    let randshu = Utils.getRangeRandom(0, 1)
                    if (gameNetWorkState.Instance.getShow() && randshu == 0) {
                        SDKManager.getInstance().showRewardVideos(null,null,()=>{
                            storage.Instance.setTiShi(storage.Instance.getTiShi() + 1)
                            TipsManager.Instance.createTips('道具已发放')
                        }, () => { })
                    }
                }
            } else {
                this.jiesuo.active = !0
                this.jiesuoIndex = ind
            }
        }
    }

    /**
     * 解锁界面按钮
     * @param t 
     * @param e 
     */
    jiesuoBtnClick(t, e) {
        var self = this
        if (e == 'no') {
            this.jiesuo.active = !1
        } else if (e == 'ling') {
            let _data = storage.Instance.getCheckPoint_diff()
            SDKManager.getInstance().showRewardVideos(null,null,()=>{
                if (PlayData.Instance.gameType == GameType.finddifference_zhaobutong) {
                    let kk = (this.cellnum * this.nowPageIndex) + self.jiesuoIndex
                    _data[3][kk] = 0
                    storage.Instance.setCheckPoint_diff(_data)
                    self.jiesuo.active = !1
                    let item = cc.find('' + self.jiesuoIndex, self.content1)
                    cc.find('suo', item).active = !1
                    cc.find('wenhao', item).active = !0
                }
            }, () => { })
        }
    }

    // update (dt) {}

    closeBtn() {
        gameShareAdsVideo.Instance.showCustomAd(CustomAdType.Shu)
        this.node.destroy()
    }


    /**
     * 打开原生矩阵广告
     */
    openMoreGamePanel() {
        this.moregamepanel.active = !0
        gameShareAdsVideo.Instance.showCustomAd(CustomAdType.More)
    }

    /**
     * 关闭更多游戏界面
     */
    closeMorePanelBtn() {
        this.moregamepanel.active = !1
        gameShareAdsVideo.Instance.hideCustomAd(CustomAdType.More)
    }
}
